package com.learn.quartz.controller;


import com.learn.quartz.service.ScheduleJobService;
import org.quartz.SchedulerConfigException;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 定时任务控制器
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/10/28 20:05
 * @updateUser: MI
 * @updateDate: 2023/10/28 20:05
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@RestController
@RequestMapping("/job")
public class ScheduleJobController {

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 添加新的Job
     *
     * @param taskId
     * @return
     */
    @GetMapping("/load/{taskId}")
    public String loadJob(@PathVariable("taskId") String taskId) {
        try {
            scheduleJobService.loadJob(taskId);
        } catch (SchedulerConfigException e) {
            return "导入定时任务失败";
        }
        return "成功";
    }

    /**
     * 恢复定时任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/resume/{taskId}")
    public String resumeJob(@PathVariable("taskId") String taskId) {
        try {
            scheduleJobService.resumeJob(taskId);
        } catch (SchedulerException e) {
            return "恢复定时任务失败";
        }
        return "成功";
    }

    /**
     * 暂停任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/stop/{taskId}")
    public String stopJob(@PathVariable("taskId") String taskId) {
        try {
            scheduleJobService.stopJob(taskId);
        } catch (SchedulerException e) {
            return "暂停定时任务失败";
        }
        return "成功";
    }

    /**
     * 卸载任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/unload/{taskId}")
    public String unloadJob(@PathVariable("taskId") String taskId) {
        try {
            scheduleJobService.unloadJob(taskId);
        } catch (SchedulerException e) {
            return "卸载定时任务失败";
        }
        return "成功";
    }

}
